package com.edu.koTA07.takingOrder;

//http://stackoverflow.com/questions/3349718/sqlite-android-login-and-register
//http://stackoverflow.com/questions/4934305/android-user-log-in

import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.edu.koTA07.XmlDataSet.SalesXmlDataSet;
import com.edu.koTA07.utils.GlobalVar;
import com.edu.koTA07.xmlreader.SalesXMLtoDataSet;



public class Login1 extends Activity {


/** Called when the activity is first created. */

 private EditText Username;
 private EditText Password;
 private Button btnLogin;
 private TextView lblResult;
 ProgressDialog dialog;
 SalesXmlDataSet sales = null;
 private ArrayList<String> listUsername;
 private ArrayList<String> listPassword;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);
        // Get the EditText and Button References
        Username = (EditText)findViewById(R.id.Username);
        Password = (EditText)findViewById(R.id.Password);
        btnLogin = (Button)findViewById(R.id.submitlogin);
        lblResult = (TextView)findViewById(R.id.result);

        //Cursor c = (Cursor) db.getAllTitles();

        //Button btnArrival = (Button) findViewById(R.id.btnRegister);
        //btnArrival.setOnClickListener(this);
        InputStream in = this.getResources().openRawResource(R.raw.sales);
		SalesXMLtoDataSet salesXMLtoBean = new SalesXMLtoDataSet(in);
		sales = salesXMLtoBean.getSalesList();
		listUsername = sales.getUsername();
		listPassword = sales.getPassword();

        // Set Click Listener
        btnLogin.setOnClickListener(new OnClickListener() {
	    	
		    public void onClick(View v) {
		    		// Check Login
		    	String username = Username.getText().toString();
		    	String password = Password.getText().toString();
		    	boolean successLogin=true;
		    	for(int i = 0;i < listUsername.size();i++)
		    	{
			    	if(username.equals(listUsername.get(i)) && password.equals(listPassword.get(i)))
			    	{
			    		
			    		Intent intent=new Intent(Login1.this,Main.class);
			    		GlobalVar.id_Sales= sales.getIDSales().get(i);
			    		startActivity(intent);
			    		finish();
			    		successLogin=false;
			    	}
			    	
			    }
		    	if(successLogin){
	    			lblResult.setText("Login failed. Username and/or password doesn't match.");
	    		}
	    	}
    	
        });
    }
    
}

